package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;

import org.come.bean.JadeorGoodstableBean;
import org.come.entity.PartJade;
import org.come.mouslisten.ShowAllSuitMouslisten;
import org.come.mouslisten.YuFuGoodsMouslisten;
import org.come.until.*;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.WorkshopBtn;
import com.tool.btn.goodbtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;

 

public class CollectionJadeJpanel extends JPanel {

    private WorkshopBtn workshopBtn,// 收录按钮
            workshopBtn2;// 获得按钮
    private JLabel labTzyf,// 放置套装玉符
            labShowAll;// 选择是否展示所有

    public static BigDecimal money,// 所需金钱
            sxlxz;// 所需灵修值

    private JScrollPane jScrollPane, // 套装列表滚动条
            jScrollPane2; // 套装介绍|玉符数量 滚动条
    private JTree jTree; // 套装列表
    private DefaultMutableTreeNode top; // 总节点
    public static boolean showAll = true;// 判断是否只显示已有玉符的套装（默认：全部显示）

    private JLabel[] GoodsListLabel = new JLabel[24]; // 用来存放24个物品表格
    private YuFuGoodsMouslisten[] goodsMouslistens = new YuFuGoodsMouslisten[24];// 用来存放24个物品的格子监听
    // 放置4个背包切换按钮
    private goodbtn[] btnrights;
    private boolean shoptext = false;// 判断是否显示预选框描述
    private int goodPosition;// 判断预选框的位置

    private JadeorGoodstableBean goodstableBean = new JadeorGoodstableBean(); // 存放要收录的玉符

    public CollectionJadeJpanel() {

        this.setPreferredSize(new Dimension(558, 495));
        this.setLayout(null);
        this.setOpaque(false);

        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 64);
        offBtn.setBounds(552 - 37, 10, 25, 25);
        this.add(offBtn);

        // 收录按钮
        workshopBtn = new WorkshopBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "收录", 8);
        workshopBtn.setBounds(93, 446, 59, 24);
        this.add(workshopBtn);

        // 获得按钮
        workshopBtn2 = new WorkshopBtn("skin/8100226/button/2.png", 1, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "获得", 11);
        workshopBtn2.setBounds(163, 422, 34, 17);
        this.add(workshopBtn2);

        // 放置套装玉符
        labTzyf = new JLabel();
        labTzyf.setBounds(98, 277, 54, 51);
        this.add(labTzyf);

        // 选择是否展示所有
        labShowAll = new JLabel("", SwingConstants.CENTER);
        labShowAll.setBounds(48, 257, 15, 15);
        labShowAll.addMouseListener(new ShowAllSuitMouslisten(null, this, null, null));
        this.add(labShowAll);

        top = new DefaultMutableTreeNode("");
        // 循环放上套装面板的方法
        AccessSuitMsgUntil.showSuitMethod(top, showAll);

        // 套装列表
        jTree = new JTree(top);
        jTree.setOpaque(false);
        jTree.putClientProperty("JTree.lineStyle", "None"); // 去除jtree的线条
        ((BasicTreeUI) jTree.getUI()).setLeftChildIndent(0); // 设置父节点与子节点左对齐，并去除缩进
        DefaultTreeCellRenderer cellRenderer = new DefaultTreeCellRenderer() {
            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
                                                          boolean leaf, int row, boolean hasFocus) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);

                // 判断是几级的叶子
                if (node.getLevel() == 2) {
                    int suitid = AccessSuitMsgUntil.returnSuitID(node.getParent().toString());// 套装id
                    int partId = AccessSuitMsgUntil.returnPartsID(node.getUserObject().toString());// 部件id
                    // 根据套装id和部件id获得玉符情况
                    PartJade jade = RoleData.getRoleData().getPackRecord().getPartJade(suitid, partId);

                    if (jade.isJade()) {
                        setForeground(new Color(102, 102, 102));
                    } else {
                        setForeground(new Color(213, 210, 209));
                    }
                }
                return this;
            }
        };
        cellRenderer.setLeafIcon(null); // 设置叶子图标
        try {
            cellRenderer.setOpenIcon(CutButtonImage.cuts("skin/8100226/button/B108.png")[0]);// 设置打开子节点图标
            cellRenderer.setClosedIcon(CutButtonImage.cuts("skin/8100226/button/B109.png")[0]); // 设置关闭子节点图标
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        cellRenderer.setFont(UIUtils.TEXT_FONT1);// 设置字体.
        cellRenderer.setBackgroundNonSelectionColor(UIUtils.Color_BACK);// 设置非选定节点的背景色
        cellRenderer.setBackgroundSelectionColor(new Color(99, 93, 90));// 设置节点在选中状态下的背景色
        cellRenderer.setBorderSelectionColor(UIUtils.Color_BACK);// 设置选中状态下节点边框的颜色

        /*
         * 设置选时或不选时，文字的变化颜色
         */
        cellRenderer.setTextNonSelectionColor(Color.white);// 设置绘制未选中状态下节点文本的颜色
        cellRenderer.setTextSelectionColor(Color.white);// 设置绘制选中状态下节点文本的颜色
        jTree.setCellRenderer(cellRenderer);
        jTree.setRootVisible(false); // 隐藏根节点
        jTree.setRowHeight(20); // 设置节点行高
        // 单击展开子节点
        jTree.addMouseListener(new MouseAdapter() { // 添加鼠标事件处理
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e)) { // 点击了鼠标左键
                    TreePath path = jTree.getSelectionPath();
                    if (path != null) {
                        if (jTree.isExpanded(path)) {
                            jTree.collapsePath(path);// 关闭节点
                        } else {
                            jTree.expandPath(path);// 展开节点
                        }
                    }

                }
            }
        });
        jTree.addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent e) {

                JTree tree = (JTree) e.getSource();
                // 利用JTree的getLastSelectedPathComponent()方法取得目前选取的节点.
                DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
                if (selectionNode == null) {
                    jScrollPane2.setViewportView(null);
                    return;
                }
                String nodeName = selectionNode.toString();
                if (selectionNode.isLeaf()) {// 判断是否是叶子节点
                    int suitid = AccessSuitMsgUntil.returnSuitID(selectionNode.getParent().toString());// 套装id
                    int partId = AccessSuitMsgUntil.returnPartsID(selectionNode.getUserObject().toString());// 部件id
                    // 判断是否有玉符
                    PartJade jade = RoleData.getRoleData().getPackRecord().getPartJade(suitid, partId);
                    StorageJadeJpanel2 jadeJpanel = new StorageJadeJpanel2();
                    StorageJadeJpanel2.partJade = jade;
                    jScrollPane2.setViewportView(jadeJpanel);

                } else {
                    int key = AccessSuitMsgUntil.returnSuitID(nodeName);
                    RichLabel label = new RichLabel(UserMessUntil.getAllSuit().getRolesuit().get(key).getIntroduce(),
                            UIUtils.TEXT_FONT);
                    Dimension d = label.computeSize(270);
                    label.setSize(d);
                    label.setPreferredSize(d);
                    jScrollPane2.setViewportView(label);
                }
            }
        });
        // 套装列表滚动条
        jScrollPane = new JScrollPane(jTree);
        jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
        jScrollPane.getViewport().setOpaque(false);
        jScrollPane.setOpaque(false);
        jScrollPane.setBounds(51, 63, 163, 188);
        jScrollPane.setBorder(BorderFactory.createEmptyBorder());
        jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(jScrollPane);

        // 套装列表滚动条
        jScrollPane2 = new JScrollPane();
        jScrollPane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane2.getVerticalScrollBar().setUI(new SrcollPanelUI());
        jScrollPane2.getVerticalScrollBar().setUnitIncrement(30);
        jScrollPane2.getViewport().setOpaque(false);
        jScrollPane2.setOpaque(false);
        jScrollPane2.setBounds(219, 45, 306, 206);
        jScrollPane2.setBorder(BorderFactory.createEmptyBorder());
        jScrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(jScrollPane2);

        // 存放物品的表格1~24
        for (int i = 0; i < 24; i++) {
            int row = i % 6;
            int col = i / 6;
            GoodsListLabel[i] = new JLabel();
            GoodsListLabel[i].setBounds(216 + row * 51, 261 + col * 51, 49, 49);
            goodsMouslistens[i] = new YuFuGoodsMouslisten(i, null);
            GoodsListLabel[i].addMouseListener(goodsMouslistens[i]);
            this.add(GoodsListLabel[i]);
        }

        // 背包四个页数的按钮
        btnrights = new goodbtn[6];
        for (int i = 0; i < btnrights.length; i++) {
            btnrights[i] = new goodbtn("skin/8100226/newbutton/C0"+(i+1)+".png", 0, this, i);
            btnrights[i].setBounds(523, 266 + i * 35, 35, 31);
            this.add(btnrights[i]);
        }
    }

    /**
     * 重置可用按钮类型
     */
    public void btntype(int path) {
        for (int i = 0; i < path; i++) {
            btnrights[i].btntypechange(2);
        }
    }

    /**
     * 画出预选边框
     *
     * @return
     */
    public void PaintingText(int goodPosition) {
        // 计算出物品描述的位置
        this.goodPosition = goodPosition;
        shoptext = true;
    }

    /**
     * 清除预选边框
     *
     * @return
     */
    public void ClearText() {
        shoptext = false;
    }

    private ImageIcon icon, icon1;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (icon == null) {
            icon = new ImageIcon("inkImg/old/background/S98.png");
        }
        if (icon1 == null) {
            icon1 = CutButtonImage.getImage("skin/8100226/button/B88.png", -1, -1);
        }
        // 背景
        g.drawImage(icon.getImage(), 0, 0, 552, 495, this);

        // 画物品的图片和数量
        GoodsListFromServerUntil.draw(g, 216, 261);
        // 画所需金钱
        if (money != null) {
            Util.drawPrice(g, money, 56, 365);
        }
        g.setFont(UIUtils.TEXT_FONT1);
        g.setColor(Color.white);
        // 画所需灵修值
        if (sxlxz != null) {
            g.drawString(sxlxz.toString(), 56, 401);
        }
        // 画拥有的灵修值
        if (RoleData.getRoleData().getLoginResult().getScoretype("灵修值") != null) {
            g.drawString(RoleData.getRoleData().getLoginResult().getScoretype("灵修值") + "", 56, 436);
        }
        if (!showAll) {
            g.drawImage(icon1.getImage(), 48, 257, 15, 15, this);
        }
    }

    /**
     * 清空界面
     */
    public void clearInterface() {
        labTzyf.setIcon(null);
        sxlxz = null;
        money = null;
        goodstableBean.setPartJade(null);
        goodstableBean.setType(0);
    }

    public JLabel[] getGoodsListLabel() {
        return GoodsListLabel;
    }

    public void setGoodsListLabel(JLabel[] goodsListLabel) {
        GoodsListLabel = goodsListLabel;
    }

    public goodbtn[] getBtnrights() {
        return btnrights;
    }

    public void setBtnrights(goodbtn[] btnrights) {
        this.btnrights = btnrights;
    }

    public WorkshopBtn getWorkshopBtn() {
        return workshopBtn;
    }

    public void setWorkshopBtn(WorkshopBtn workshopBtn) {
        this.workshopBtn = workshopBtn;
    }

    public WorkshopBtn getWorkshopBtn2() {
        return workshopBtn2;
    }

    public void setWorkshopBtn2(WorkshopBtn workshopBtn2) {
        this.workshopBtn2 = workshopBtn2;
    }

    public JLabel getLabTzyf() {
        return labTzyf;
    }

    public void setLabTzyf(JLabel labTzyf) {
        this.labTzyf = labTzyf;
    }

    public JScrollPane getjScrollPane2() {
        return jScrollPane2;
    }

    public void setjScrollPane2(JScrollPane jScrollPane2) {
        this.jScrollPane2 = jScrollPane2;
    }

    public JTree getjTree() {
        return jTree;
    }

    public void setjTree(JTree jTree) {
        this.jTree = jTree;
    }

    public DefaultMutableTreeNode getTop() {
        return top;
    }

    public void setTop(DefaultMutableTreeNode top) {
        this.top = top;
    }

    public JScrollPane getjScrollPane() {
        return jScrollPane;
    }

    public void setjScrollPane(JScrollPane jScrollPane) {
        this.jScrollPane = jScrollPane;
    }

    public YuFuGoodsMouslisten[] getGoodsMouslistens() {
        return goodsMouslistens;
    }

    public void setGoodsMouslistens(YuFuGoodsMouslisten[] goodsMouslistens) {
        this.goodsMouslistens = goodsMouslistens;
    }

    public JadeorGoodstableBean getGoodstableBean() {
        return goodstableBean;
    }

    public void setGoodstableBean(JadeorGoodstableBean goodstableBean) {
        this.goodstableBean = goodstableBean;
    }
}
